package cn.tedu.micro.demo.rabbit.controller;

import cn.tedu.micro.demo.rabbit.amqp.AmqpProducerClient;
import cn.tedu.micro.demo.rabbit.config.DeclareConfiguration;
import cn.tedu.micro.demo.rabbit.pojo.MyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class SendController {
    //发送消息到交换机 到队列
    /*@Autowired
    private RabbitTemplate rabbitTemplate;*/
    @Autowired
    private AmqpProducerClient amqpProducerClient;
    /**
     * GET
     * /send
     * String message
     * "ok"
     */
    @GetMapping("/send")
    public String send(String message){
        //1.第一种发送方式 让rabbit客户端帮助我们实现序列化 没有特殊的属性携带
        //channel.basicPublish("ex01","",null,message.getBytes(StandardCharsets.UTF_8));
        //rabbitTemplate.convertAndSend("ex01","",message);
        //2.第二种发送方式,自定义封装message对象 做body序列化 props携带
        /*MessageProperties props = new MessageProperties();
        //设置消息携带的属性 比如epiration
        props.setExpiration("30000");
        byte[] body=message.getBytes();
        Message msg=new Message(body,props);
        rabbitTemplate.convertAndSend("ex01","",msg);*/
        MyMessage myMessage=new MyMessage();
        myMessage.setMessage(message);
        myMessage.setSendTime(new Date());
        amqpProducerClient.sendDelay("ex01",myMessage,30000);
        return "ok";
    }
    @GetMapping("/send/delay")
    public String sendDelay(String message){
        MyMessage myMessage=new MyMessage();
        myMessage.setMessage(message);
        myMessage.setSendTime(new Date());
        amqpProducerClient.sendDelay(DeclareConfiguration.TRANS_EX,myMessage,30000);
        return "ok";
    }
}
